<%@ page language="java" import="com.alibaba.fastjson.JSONObject" pageEncoding="UTF-8" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.jobs.hggd.duss.utils.HggdUtil" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%--手动推送收文数据--%>
<%
    final String TABLENAME = "uf_swsxjc";
    final int FORMID = 50;
    final int SUCCESS = 0;
    final int FAILURE = 1;
    int succcount = 0;
    BaseBean baseBean = new BaseBean();
    String ids = Util.null2String(request.getParameter("ids"));
    baseBean.writeLog(">>>>>pushSwData_Ajax>>>>>ids=" + ids);
    RecordSet rs = new RecordSet();
    HggdUtil hggdUtil = new HggdUtil();
    JSONObject jsonData = new JSONObject();
    int czzt = FAILURE;
    String message = "";
    int seccategory = hggdUtil.getDoccategory(FORMID);
    String url = baseBean.getPropValue("arc4check", "url");
    String sql = "select * from " + TABLENAME + " where (czzt=1 or czzt is null)";
    if (!ids.isEmpty()) {
        sql += " and id in (" + ids + ")";
    }
    rs.executeQuery(sql);
    int totalcount = rs.getCounts();
    while (rs.next()) {
        Map<String, Object> fileDataMap = new HashMap<>();
        String requestid = Util.null2String(rs.getString("lcid"));//新增流程ID
        baseBean.writeLog(">>>>>pushFwData_Ajax>>>>>requestid=" + requestid);
        String requestname = Util.null2String(rs.getString("lcmc"));//新增流程名称
        fileDataMap.put("swnf", Util.null2String(rs.getString("swnf"), " "));
        fileDataMap.put("bgqxwb", Util.null2String(rs.getString("bgqxwb")," "));
        String zwwj = Util.null2String(rs.getString("zwwj"));
        String fj = Util.null2String(rs.getString("fj"));
        String bdjqzyjfj = hggdUtil.getBdToPdfFile(FORMID, requestid);
        fileDataMap.put("djrwb", Util.null2String(rs.getString("djrwb")));
        fileDataMap.put("djrq", Util.null2String(rs.getString("djrq")));
        fileDataMap.put("lwdw", Util.null2String(rs.getString("lwdw")));
        fileDataMap.put("lwwh", Util.null2String(rs.getString("lwwh")));
        fileDataMap.put("wjlx", "收文");
        fileDataMap.put("lwxcrq", Util.null2String(rs.getString("lwxcrq")));
        fileDataMap.put("hjcdwb", Util.null2String(rs.getString("hjcdwb")," "));
        fileDataMap.put("cbrq", Util.null2String(rs.getString("cbrq")));
        fileDataMap.put("wjyszs", Util.null2String(rs.getString("wjyszs")));
        fileDataMap.put("wjfszs", Util.null2String(rs.getString("wjfszs")));
        fileDataMap.put("fjfszs", Util.null2String(rs.getString("fjfszs")));
        fileDataMap.put("zwwjwb", Util.null2String(rs.getString("zwwjwb"), " "));
        fileDataMap.put("fjwb", Util.null2String(rs.getString("fjwb"), " "));
        fileDataMap.put("flmc", Util.null2String(rs.getString("flmc"), " "));
        fileDataMap.put("flmcwb", Util.null2String(rs.getString("flmcwb"), " "));
        fileDataMap.put("djbmwb", Util.null2String(rs.getString("djbmwb"), " "));
        fileDataMap.put("fszs", Util.null2String(rs.getString("fszs")));
        fileDataMap.put("bz", Util.null2String(rs.getString("bz"), " "));
        fileDataMap.put("mjwb", Util.null2String(rs.getString("mjwb"), " "));
        String target = "/";
        String baseDirPath = baseBean.getPropValue("arc4check", "basedirpath");
        String appid = "oa";
        try {
            String md5 = hggdUtil.generateMD5(requestid);
            String tarDirName = appid + "-" + hggdUtil.getDateDir("") + "-" + md5;
            String fullTarDirPath = baseDirPath + appid + target + hggdUtil.getDateDir(target) + target + tarDirName;
            String fullTarDirName = fullTarDirPath + ".tar";
            String timestamp = hggdUtil.getTimestamp();
            String filepath = "";
            List<Map<String, Object>> imagefileList = new ArrayList<>();
            //获取正文文件信息
            if (!zwwj.isEmpty()) {
                String[] docidList = zwwj.split(",");
                for (String docid : docidList) {
                    Map<String, Object> docidMap = hggdUtil.getImagefileInfo(docid);
                    String imagefileid = Util.null2String(docidMap.get("imagefileid"));
                    String operatedate = Util.null2String(docidMap.get("operatedate"));
                    String operatetime = Util.null2String(docidMap.get("operatetime"));
                    String imagefilename = Util.null2String(docidMap.get("imagefilename"));
                    String imagefiletype = Util.null2String(docidMap.get("imagefiletype"));
                    String filesize = Util.null2String(docidMap.get("filesize"));
                    InputStream inputStream = hggdUtil.getInputStreamById(imagefileid);
                    hggdUtil.createFile(inputStream, fullTarDirPath + target + timestamp, imagefilename);
                    String filetype = "";
                    String filename = "";
                    if (imagefilename.lastIndexOf(".") > -1) {
                        filetype = imagefilename.substring(imagefilename.lastIndexOf(".") + 1);
                        filename = imagefilename.substring(0, imagefilename.lastIndexOf("."));
                    }
                    Map<String, Object> imagefileMap = new HashMap<>();
                    imagefileMap.put("docid", docid);
                    imagefileMap.put("imagefileid", imagefileid);
                    imagefileMap.put("imagefiletype", imagefiletype);
                    imagefileMap.put("filetype", filetype);
                    imagefileMap.put("filename", filename);
                    imagefileMap.put("imagefilename", imagefilename);
                    imagefileMap.put("filesize", filesize);
                    imagefileMap.put("doccreatedate", operatedate);
                    imagefileMap.put("doccreatetime", operatetime);
                    baseBean.writeLog(">>>>>pushSwData_Ajax>>>>>imagefileMap=" + imagefileMap);
                    imagefileList.add(imagefileMap);
                    filepath += "".equals(filepath) ? timestamp + target + imagefilename : "," + timestamp + target + imagefilename;
                }
            }
            //获取附件文件信息
            if (!fj.isEmpty()) {
                String[] jfList = fj.split(",");
                for (String docid : jfList) {
                    Map<String, Object> docidMap = hggdUtil.getImagefileInfo(docid);
                    String imagefileid = Util.null2String(docidMap.get("imagefileid"));
                    String operatedate = Util.null2String(docidMap.get("operatedate"));
                    String operatetime = Util.null2String(docidMap.get("operatetime"));
                    String imagefilename = Util.null2String(docidMap.get("imagefilename"));
                    String imagefiletype = Util.null2String(docidMap.get("imagefiletype"));
                    String filesize = Util.null2String(docidMap.get("filesize"));
                    InputStream inputStream = hggdUtil.getInputStreamById(imagefileid);
                    hggdUtil.createFile(inputStream, fullTarDirPath + target + timestamp, imagefilename);
                    String filetype = "";
                    String filename = "";
                    if (imagefilename.lastIndexOf(".") > -1) {
                        filetype = imagefilename.substring(imagefilename.lastIndexOf(".") + 1);
                        filename = imagefilename.substring(0, imagefilename.lastIndexOf("."));
                    }
                    Map<String, Object> imagefileMap = new HashMap<>();
                    imagefileMap.put("docid", docid);
                    imagefileMap.put("imagefileid", imagefileid);
                    imagefileMap.put("imagefiletype", imagefiletype);
                    imagefileMap.put("filetype", filetype);
                    imagefileMap.put("filename", filename);
                    imagefileMap.put("imagefilename", imagefilename);
                    imagefileMap.put("filesize", filesize);
                    imagefileMap.put("doccreatedate", operatedate);
                    imagefileMap.put("doccreatetime", operatetime);
                    baseBean.writeLog(">>>>>pushSwData_Ajax>>>>>imagefileMap=" + imagefileMap);
                    imagefileList.add(imagefileMap);
                    filepath += "".equals(filepath) ? timestamp + target + imagefilename : "," + timestamp + target + imagefilename;
                }
            }
            //获取表单签字意见转pdf信息
            if (!bdjqzyjfj.isEmpty()) {
                String[] bdjqzyjfjList = bdjqzyjfj.split(",");
                for (String docid : bdjqzyjfjList) {
                    Map<String, Object> docidMap = hggdUtil.getImagefileInfo(docid);
                    String imagefileid = Util.null2String(docidMap.get("imagefileid"));
                    String operatedate = Util.null2String(docidMap.get("operatedate"));
                    String operatetime = Util.null2String(docidMap.get("operatetime"));
                    String imagefilename = Util.null2String(docidMap.get("imagefilename"));
                    String imagefiletype = Util.null2String(docidMap.get("imagefiletype"));
                    String filesize = Util.null2String(docidMap.get("filesize"));
                    InputStream inputStream = hggdUtil.getInputStreamById(imagefileid);
                    hggdUtil.createFile(inputStream, fullTarDirPath + target + timestamp, imagefilename);
                    String filetype = "";
                    String filename = "";
                    if (imagefilename.lastIndexOf(".") > -1) {
                        filetype = imagefilename.substring(imagefilename.lastIndexOf(".") + 1);
                        filename = imagefilename.substring(0, imagefilename.lastIndexOf("."));
                    }
                    Map<String, Object> imagefileMap = new HashMap<>();
                    imagefileMap.put("docid", docid);
                    imagefileMap.put("imagefileid", imagefileid);
                    imagefileMap.put("imagefiletype", imagefiletype);
                    imagefileMap.put("filetype", filetype);
                    imagefileMap.put("filename", filename);
                    imagefileMap.put("imagefilename", imagefilename);
                    imagefileMap.put("filesize", filesize);
                    imagefileMap.put("doccreatedate", operatedate);
                    imagefileMap.put("doccreatetime", operatetime);
                    baseBean.writeLog(">>>>>pushSwData_Ajax>>>>>imagefileMap=" + imagefileMap);
                    imagefileList.add(imagefileMap);
                    filepath += "".equals(filepath) ? timestamp + target + imagefilename : "," + timestamp + target + imagefilename;
                }
            }
            fileDataMap.put("requestid", hggdUtil.generateMD5(requestid));
            fileDataMap.put("requestname", requestname);
            fileDataMap.put("imagefileList", imagefileList);
            fileDataMap.put("destFilePath", fullTarDirPath + target + timestamp + target + timestamp + ".xml");
            baseBean.writeLog(">>>>>pushSwData_Ajax>>>>>fileDataMap=" + fileDataMap);
            if (!bdjqzyjfj.isEmpty() && !zwwj.isEmpty()) {
                //生成xml文件
                boolean fileDataStatus = hggdUtil.fileDataToXml(fileDataMap);
                Map<String, Object> archiveMap = new HashMap<>();
                archiveMap.put("dataXmlPath", timestamp + target + timestamp + ".xml");
                archiveMap.put("filePath", filepath);
                archiveMap.put("destFilePath", fullTarDirPath + target + "档案包目录.xml");
                baseBean.writeLog(">>>>>pushSwData_Ajax>>>>>archiveMap=" + archiveMap);
                //生成目录xml文件
                boolean archiveDirStatus = hggdUtil.archiveDirectoryToXml(archiveMap);
                //生成tar包
                boolean createTarStatus = hggdUtil.createTarFromDirectory(fullTarDirPath, fullTarDirName);
                baseBean.writeLog(">>>>>pushSwData_Ajax>>>>>fileDataStatus=" + fileDataStatus);
                baseBean.writeLog(">>>>>pushSwData_Ajax>>>>>archiveDirStatus=" + archiveDirStatus);
                baseBean.writeLog(">>>>>pushSwData_Ajax>>>>>createTarStatus=" + createTarStatus);
                if (fileDataStatus && archiveDirStatus && createTarStatus) {
                    int docids = hggdUtil.createDocid(fullTarDirName, fullTarDirName, seccategory);
                    if (docids <= 0) {
                        message = "生成文档失败";
                    } else {
                        rs.executeUpdate("update " + TABLENAME + " set gdxxb=? where lcid=?", docids, requestid);
                        JSONObject paramJson = new JSONObject();
                        paramJson.put("reqid", requestid);
                        paramJson.put("appid", "OA");
                        paramJson.put("apptoken", "");
                        paramJson.put("companycode", "");
                        paramJson.put("fondsname", "");
                        Map<String, Object> docidMap = hggdUtil.getImagefileInfo(docids + "");
                        String imagefileid = Util.null2String(docidMap.get("imagefileid"));
                        String ddcode = hggdUtil.getTokenKey(imagefileid);
                        String tarfilePath = baseBean.getPropValue("arc4check", "oaurl") + "?fileid=" + imagefileid + "&download=1&ddcode=" + ddcode;
                        paramJson.put("tarfilefetch", tarfilePath);
                        paramJson.put("aiptitle", tarDirName + ".tar");
                        paramJson.put("fileid", timestamp);
                        paramJson.put("model", "f");
                        paramJson.put("aipdigest", hggdUtil.generateMD5(requestid));
                        JSONObject resultJson = hggdUtil.sendOKHttpRequest(paramJson, url);
                        baseBean.writeLog(">>>>>pushSwData_Ajax>>>>>resultJson=" + resultJson);
                        JSONObject dataJson = resultJson.getJSONObject("data");
                        String code = Util.null2String(dataJson.getString("code"));
                        if (!"200".equals(code)) {
                            String desc = Util.null2String(dataJson.getString("desc"));
                            message = "档案系统接口返回：" + desc;
                        } else {
                            czzt = SUCCESS;
                            message = "档案系统接口接收成功";
                            succcount++;
                        }
                    }
                } else {
                    message = "创建档案归档信息包失败";
                }
            }else {
                message = "表单转PDF文件未生成或正文未上传";
            }
        } catch (Exception e) {
            message = "接口处理异常,查看日志搜索关键字>>>>>pushSwData_Ajax>>>>>Exception=";
            baseBean.writeLog(">>>>>pushSwData_Ajax>>>>>Exception=" + e);
        }
        rs.executeUpdate("update " + TABLENAME + " set czzt=?,msg=? where lcid=?", czzt, message, requestid);
    }
    jsonData.put("totalcount", totalcount);
    jsonData.put("succcount", succcount);
    jsonData.put("failcount", totalcount - succcount);
    out.print(jsonData);
%>